package service

import (
	"context"
	"errors"
	"github.com/kuangshp/go-utils/k"
	"gorm.io/gorm"
	"insighttracking/query/dao"
	"insighttracking/query/model"
	"strings"
)

type _shelfService struct{}

func (s _shelfService) CreateApi(ctx context.Context, req model.ShelfEntity) error {
	_, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(req.EnterpriseID), dao.ShelfEntity.WarehouseID.Eq(req.WarehouseID), dao.ShelfEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("名字已经存在,不能重复")
	}

	if err = dao.ShelfEntity.WithContext(ctx).Create(&model.ShelfEntity{
		WarehouseID:     req.WarehouseID,     // 关联到warehouse表主键id
		WarehouseAreaID: req.WarehouseAreaID, //关联到warehouse_area表主键id
		EnterpriseID:    req.EnterpriseID,    // 关联到enterprise表主键id
		Name:            req.Name,            // 仓库名称
		Code:            req.Code,            // 仓库编码
	}); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _shelfService) DeleteByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	if _, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _shelfService) GetListApi(ctx context.Context, enterpriseId int64, warehouseId int64, warehouseAreaId int64, keyword string) ([]*model.ShelfEntity, error) {
	tx := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId),
		dao.ShelfEntity.Status.Eq(0),
		dao.ShelfEntity.WarehouseID.Eq(warehouseId),
		dao.ShelfEntity.WarehouseAreaID.Eq(warehouseAreaId),
	)
	if keyword != "" {
		keyword = strings.ReplaceAll(keyword, "%", "\\%")
		keyword = strings.ReplaceAll(keyword, "_", "\\%")
		tx = tx.Where(dao.ShelfEntity.Name.Like("%" + keyword + "%")).Or(dao.ShelfEntity.Code.Like("%" + keyword + "%"))
	}
	return tx.Order(dao.ShelfEntity.ID.Asc()).Find()
}

func (s _shelfService) ModifyStatusByIdApi(ctx context.Context, id int64) error {
	entity, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("传递的id错误")
	}
	if _, err = dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).UpdateSimple(
		dao.ShelfEntity.Status.Value(int64(k.If(entity.Status == 0, 1, 0))),
	); err != nil {
		return errors.New("修改失败")
	}
	return nil
}

func (s _shelfService) ModifyByIdApi(ctx context.Context, id int64, enterpriseId int64, req model.ShelfEntity) error {
	shelfEntity, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id), dao.ShelfEntity.EnterpriseID.Eq(enterpriseId)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("货架不存在")
	}
	sameNameEntity, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.WarehouseID.Eq(shelfEntity.WarehouseID), dao.ShelfEntity.Name.Eq(req.Name)).First()
	if !errors.Is(err, gorm.ErrRecordNotFound) && id != sameNameEntity.ID {
		return errors.New("名字已经存在,不能重复")
	}
	if _, err = dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).UpdateSimple(
		dao.ShelfEntity.Name.Value(req.Name), // 仓库名称
		dao.ShelfEntity.Code.Value(req.Code), // 仓库编码
	); err != nil {
		return errors.New("创建失败")
	}
	return nil
}

func (s _shelfService) ModifyDefaultByIdApi(ctx context.Context, enterpriseId int64, id int64) error {
	entity, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).First()
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return errors.New("设置默认库区失败")
	}
	if _, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfEntity.WarehouseID.Eq(entity.WarehouseID)).UpdateSimple(
		dao.ShelfEntity.IsDefault.Value(0), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置默认库区失败")
	}
	if _, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id), dao.ShelfEntity.EnterpriseID.Eq(enterpriseId)).UpdateSimple(
		dao.ShelfEntity.IsDefault.Value(1), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置默认库区失败")
	}
	return nil
}

func (s _shelfService) SetAllDefaultApi(ctx context.Context, enterpriseId int64, warehouseId int64) error {
	if _, err := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfEntity.WarehouseID.Eq(warehouseId)).UpdateSimple(
		dao.ShelfEntity.IsDefault.Value(0), // 是否为默认仓库,0表示不是，1表示是
	); err != nil {
		return errors.New("设置其他为非默认库区失败")
	}
	return nil
}

func (s _shelfService) GetSelfByIdApi(ctx context.Context, id int64) (*model.ShelfEntity, error) {
	return dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).First()
}

func (s _shelfService) GetInfoByIdApi(ctx context.Context, id int64) (*model.ShelfEntity, error) {
	return dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.ID.Eq(id)).First()
}

func (s _shelfService) GetCountByWareHouseAreaIdApi(ctx context.Context, enterpriseId int64, warehouseAreaId int64) (int64, error) {
	return dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId), dao.ShelfEntity.WarehouseAreaID.Eq(warehouseAreaId)).Count()
}

func (s _shelfService) GetListByWarehouseIDApi(ctx context.Context, enterpriseId int64, warehouseId int64) ([]*model.ShelfEntity, error) {
	tx := dao.ShelfEntity.WithContext(ctx).Where(dao.ShelfEntity.EnterpriseID.Eq(enterpriseId),
		dao.ShelfEntity.Status.Eq(0),
		dao.ShelfEntity.WarehouseID.Eq(warehouseId),
	)
	return tx.Order(dao.ShelfEntity.IsDefault.Desc(), dao.ShelfEntity.ID.Desc()).Find()
}

var ShelfService = new(_shelfService)
